package dr.incomeapp.dao;

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.content.res.Resources;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
import android.widget.ArrayAdapter;

import com.j256.ormlite.android.apptools.OrmLiteSqliteOpenHelper;
import com.j256.ormlite.dao.Dao;
import com.j256.ormlite.support.ConnectionSource;
import com.j256.ormlite.table.TableUtils;

import dr.incomeapp.Application;
import dr.incomeapp.R;
import dr.incomeapp.bean.CategoryBean;
import dr.incomeapp.bean.IncomeBean;

public class DataBaseHelper extends OrmLiteSqliteOpenHelper {
	// name of the database file for your application -- change to something
	// appropriate for your app
	private static final String DATABASE_NAME = "INCOMEAPP";

	// any time you make changes to your database objects, you may have to
	// increase the database version
	private static final int DATABASE_VERSION = 2;

	// the DAO object we use to access the SimpleData table
	private Dao<IncomeBean, Integer> incomeDao = null;
	
	
	protected CategoryDao categoryDao;// = FactoryDao.getCategoryDao();


	public DataBaseHelper(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}
	
	public void deleteAllTables(){
		try {
			TableUtils.dropTable(connectionSource,IncomeBean.class, true);
			TableUtils.dropTable(connectionSource,CategoryBean.class, true);
		} catch (java.sql.SQLException e) {
			Log.e(DataBaseHelper.class.getName(), "Can't delete database", e);
		}
	}
	
	public void createAllTables(){
		try {
			TableUtils.createTableIfNotExists(connectionSource, IncomeBean.class);
			TableUtils.createTableIfNotExists(connectionSource, CategoryBean.class);
			
			
			String arrCategoryCost[] = Application.application.getContext().getResources().getStringArray(R.array.arrCategoryCost);
			categoryDao = FactoryDao.getCategoryDao();
			for(String c: arrCategoryCost){
				CategoryBean categoryBean = new CategoryBean();
				categoryBean.setCost(true);
				categoryBean.setName(c);
				
				categoryDao.createOrUpdate(categoryBean);
				
			}
			

			
			
			
		} catch (java.sql.SQLException e) {
			Log.e(DataBaseHelper.class.getName(), "Can't create database", e);
		}
	}

	@Override
	public void onCreate(SQLiteDatabase database,
			ConnectionSource connectionSource) {
		try {
			TableUtils.createTableIfNotExists(connectionSource, IncomeBean.class);
			TableUtils.createTableIfNotExists(connectionSource, CategoryBean.class);

		} catch (SQLException e) {
			Log.e(DataBaseHelper.class.getName(), "Can't create database", e);
			throw new RuntimeException(e);
		} catch (java.sql.SQLException e) {
			e.printStackTrace();
		}

	}

	@Override
	public void onUpgrade(SQLiteDatabase db, ConnectionSource connectionSource,
			int oldVersion, int newVersion) {
		try {
			List<String> allSql = new ArrayList<String>();
			switch (oldVersion) {
			case 1:
				// allSql.add("alter table AdData add column `new_col` VARCHAR");
				// allSql.add("alter table AdData add column `new_col2` VARCHAR");
			}
			for (String sql : allSql) {
				db.execSQL(sql);
			}
		} catch (SQLException e) {
			Log.e(DataBaseHelper.class.getName(), "exception during onUpgrade",
					e);
			throw new RuntimeException(e);
		}

	}

	public Dao<IncomeBean, Integer> getIncomeDao() {
		if (null == incomeDao) {
			try {
				incomeDao = getDao(IncomeBean.class);
			} catch (java.sql.SQLException e) {
				e.printStackTrace();
			}
		}
		return incomeDao;
	}

}